package me.chan.practice;

import java.util.Arrays;
import java.util.Comparator;

import static me.chan.util.SortUtil.show;

/**
 * Exercise 2.5.16
 * Created by JanGin on 2017/9/24.
 */
public class California {

    private static class CandidateComparator implements Comparator<String> {

        private static final String sortOrder = "RWQOJMVAHBSGZXNTCIEKUPDYFL";

        @Override
        public int compare(String s1, String s2) {
           int n = Math.min(s1.length(), s2.length());
            for (int i = 0; i < n; i++) {
                int s1_index = sortOrder.indexOf(s1.charAt(i));
                int s2_index = sortOrder.indexOf(s2.charAt(i));
                if (s1_index < s2_index) return -1;
                if (s1_index > s2_index) return +1;

            }
            return s1.length() - s2.length();
        }
    }

    public static void main(String[] args) {
        String[] candidates = {"ROBERT 'BUTCH' DOLE",
                                "ROBERT C. MANNHEIM",
                                "RONALD JASON PALMIERI",
                                "RICHARD J. SIMMONS",
                                "WARREN FARRELL",
                                "WILLIAM 'BILL' S. CHAMBERS",
                                "LAWRENCE STEVEN STRAUSS",
                                "LEO GALLAGHER"};
        Arrays.sort(candidates, new CandidateComparator());
        show(candidates);

        int[] arr = new int[4];
        int init, max, min;
        for (int i=1000; i<10000; ++i) {
            int divide = 1000;
            init = i;
            for (int j=0; j<arr.length; j++) {
                arr[j] = init / divide;
                init %= divide;
                divide /= 10;
            }
            Arrays.sort(arr);
            max = 1000 * arr[3] + 100 * arr[2] + 10 * arr[1] + arr[0];

            for (int k=0; k < arr.length; k++) {
                if (arr[0] == 0) {
                    int temp = arr[1];
                    arr[1] = arr[0];
                    arr[0] = temp;

                    break;
                }
            }

            min = 1000 * arr[0] + 100 * arr[1] + 10 * arr[2] + arr[3];

            if (max - min == i) {
                System.out.println(i);  //  6174
            }
        }
    }
}
